﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MG_POS_PC.Util;
using System.Data;
using System.Data.SqlClient;

namespace MG_POS_PC.DAL.Setting
{
    /// <summary>
    /// 数据访问类:t_network_setting
    /// </summary>
    public partial class t_network_setting
    {
        public t_network_setting()
        { }
        #region  BasicMethod


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(MG_POS_PC.Model.Setting.t_network_setting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_network_setting(");
            strSql.Append("ns_station,ns_name,ns_ip,ns_type,create_datetime,update_datetime)");
            strSql.Append(" values (");
            strSql.Append("@ns_station,@ns_name,@ns_ip,@ns_type,@create_datetime,@update_datetime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ns_station", SqlDbType.Decimal,5),
					new SqlParameter("@ns_name", SqlDbType.NVarChar,50),
					new SqlParameter("@ns_ip", SqlDbType.NVarChar,50),
					new SqlParameter("@ns_type", SqlDbType.NVarChar,2),
					new SqlParameter("@create_datetime", SqlDbType.NVarChar,50),
					new SqlParameter("@update_datetime", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.ns_station;
            parameters[1].Value = model.ns_name;
            parameters[2].Value = model.ns_ip;
            parameters[3].Value = model.ns_type;
            parameters[4].Value = model.create_datetime;
            parameters[5].Value = model.update_datetime;
            
            object obj = DBHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(MG_POS_PC.Model.Setting.t_network_setting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_network_setting set ");
            strSql.Append("ns_station=@ns_station,");
            strSql.Append("ns_name=@ns_name,");
            strSql.Append("ns_ip=@ns_ip,");
            strSql.Append("ns_type=@ns_type,");
            strSql.Append("create_datetime=@create_datetime,");
            strSql.Append("update_datetime=@update_datetime");
            strSql.Append(" where ns_no=@ns_no");
            SqlParameter[] parameters = {
					new SqlParameter("@ns_station", SqlDbType.Decimal,5),
					new SqlParameter("@ns_name", SqlDbType.NVarChar,50),
					new SqlParameter("@ns_ip", SqlDbType.NVarChar,50),
					new SqlParameter("@ns_type", SqlDbType.NVarChar,2),
					new SqlParameter("@create_datetime", SqlDbType.NVarChar,50),
					new SqlParameter("@update_datetime", SqlDbType.NVarChar,50),
					new SqlParameter("@ns_no", SqlDbType.Int,4)};
            parameters[0].Value = model.ns_station;
            parameters[1].Value = model.ns_name;
            parameters[2].Value = model.ns_ip;
            parameters[3].Value = model.ns_type;
            parameters[4].Value = model.create_datetime;
            parameters[5].Value = model.update_datetime;
            parameters[6].Value = model.ns_no;

            int rows = DBHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ns_no)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_network_setting ");
            strSql.Append(" where ns_no=@ns_no");
            SqlParameter[] parameters = {
					new SqlParameter("@ns_no", SqlDbType.Int,4)
			};
            parameters[0].Value = ns_no;

            int rows = DBHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ns_nolist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_network_setting ");
            strSql.Append(" where ns_no in (" + ns_nolist + ")  ");
            int rows = DBHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_POS_PC.Model.Setting.t_network_setting GetModel(int ns_no)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ns_no,ns_station,ns_name,ns_ip,ns_type,create_datetime,update_datetime from t_network_setting ");
            strSql.Append(" where ns_no=@ns_no");
            SqlParameter[] parameters = {
					new SqlParameter("@ns_no", SqlDbType.Int,4)
			};
            parameters[0].Value = ns_no;

            MG_POS_PC.Model.Setting.t_network_setting model = new MG_POS_PC.Model.Setting.t_network_setting();
            DataSet ds = DBHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_POS_PC.Model.Setting.t_network_setting DataRowToModel(DataRow row)
        {
            MG_POS_PC.Model.Setting.t_network_setting model = new MG_POS_PC.Model.Setting.t_network_setting();
            if (row != null)
            {
                if (row["ns_no"] != null && row["ns_no"].ToString() != "")
                {
                    model.ns_no = int.Parse(row["ns_no"].ToString());
                }
                if (row["ns_station"] != null && row["ns_station"].ToString() != "")
                {
                    model.ns_station = int.Parse(row["ns_station"].ToString());
                }
                if (row["ns_name"] != null)
                {
                    model.ns_name = row["ns_name"].ToString();
                }
                if (row["ns_ip"] != null)
                {
                    model.ns_ip = row["ns_ip"].ToString();
                }
                if (row["ns_type"] != null)
                {
                    model.ns_type = row["ns_type"].ToString();
                }
                if (row["create_datetime"] != null)
                {
                    model.create_datetime = row["create_datetime"].ToString();
                }
                if (row["update_datetime"] != null)
                {
                    model.update_datetime = row["update_datetime"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ns_no,ns_station,ns_name,ns_ip,ns_type,create_datetime,update_datetime ");
            strSql.Append(" FROM t_network_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DBHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ns_no,ns_station,ns_name,ns_ip,ns_type,create_datetime,update_datetime ");
            strSql.Append(" FROM t_network_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DBHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_network_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DBHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ns_no desc");
            }
            strSql.Append(")AS Row, T.*  from t_network_setting T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DBHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_network_setting";
            parameters[1].Value = "ns_no";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
